﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TermitesMagnets
{
    public class Board
    {
        private bool[,] occupied;
        private int width;
        private int height;
        private int cols;
        private int pixelRadius;
        private int rows;
        private int termitePerCell = 3;

        public int Cols{ get { return cols; }}
        public int Rows{ get { return rows; } set { rows = value; } }
        private List<Obstacle> obstacles;
        public List<Obstacle> Obstacles
        {
            get { return obstacles; }
        }

        public Board()
        {
            obstacles = new List<Obstacle>();
            width = Game1.GlobalGame.GraphicsDevice.Viewport.Width;
            height = Game1.GlobalGame.GraphicsDevice.Viewport.Height;
            pixelRadius = 2; //Termite.Texture.Width;
            cols = width / pixelRadius;
            rows = height / pixelRadius;
            occupied = new bool[cols, rows];
            for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    occupied[i, j] = false;
                }
            }
        }

        public void AddObstacle(Rectangle rect)
        {
            for (int col = rect.Left; col < rect.Right; col++)
            {
                for (int row = rect.Top; row < rect.Bottom; row++)
                {
                    Occupy(col, row);
                }
            }
            obstacles.Add(new Obstacle(Game1.GlobalGame, rect, this));
        }

        public bool Occupy(int col, int row)
        {
            if (col >= cols || col<0 || row >= rows || row<0)
                return false;

            if (occupied[col, row])
                return false;
            else
            {
                occupied[col, row] = true;
                return true;
            }
        }

        public bool isOccupied(int col, int row)
        {
            if (col >= cols || col < 0 || row >= rows || row < 0)
                return true;
            else
                return occupied[col, row];
        }

        public bool isFree(int col, int row)
        {
            return !isOccupied(col, row);
        }

        public void Clear(int col, int row)
        {
            occupied[col, row] = false;
        }

        #region cordinates
        public bool IsInWorld(Vector2 position)
        {
            if (position.X > cols || position.X < 0 || position.Y > rows || position.Y < 0)
                return false;
            else
                return true;
        }

        public Vector2 BringToWorld(Vector2 position)
        {
            if (position.X > cols)
                position.X = cols;
            else if (position.X < 0)
                position.X = 0;
            if (position.Y > rows)
                position.Y = rows;
            else if (position.Y < 0)
                position.Y = 0;
            return position;
        }

        public int WorldXToReal(int col){ return col * pixelRadius; }
        public int WorldYToReal(int row) { return row * pixelRadius; }
        public Vector2 WorldPositionToReal(Vector2 position)
        {
            return new Vector2((float)WorldXToReal((int)position.X), (float)WorldYToReal((int)position.Y));
        }

        public Vector2 RealPositionToWorld(Vector2 real)
        {
            return new Vector2(real.X / pixelRadius, real.Y / pixelRadius);
        }
        #endregion
    }
}
